package com.vincent.kdp.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.vincent.kdp.Activity.BumenActivity;
import com.vincent.kdp.Activity.ShangjiaActivity;
import com.vincent.kdp.R;
import com.vincent.kdp.ui.work.ShangjiaInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dzl on 2017/1/15.
 */

public class ShangjiaAdapter extends BaseAdapter {
    private List<ShangjiaInfo> mListData = new ArrayList<>();

    private final Context mContext;

    public ShangjiaAdapter(Context context){
       mContext = context;

   }

    @Override
    public int getCount() {
        if (mListData == null) {
            return 0;
        }
        return mListData.size();
    }

    @Override
    public Object getItem(int i) {
        return mListData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ShangjiaAdapter.ViewHold viewHold = null;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.shangjia_activity, null);
            viewHold = new ShangjiaAdapter.ViewHold();
            viewHold.shangjiainfo=(TextView) view.findViewById(R.id.tv_shangjia);
            view.setTag(viewHold);
        }else{
            viewHold = (ShangjiaAdapter.ViewHold) view.getTag();

        }
        final ShangjiaInfo info = mListData.get(i);
        if (info != null) {
            viewHold.shangjiainfo.setText((CharSequence) info.getXinxi());
        }
        view.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,BumenActivity.class);
                int id=info.getId();
                Bundle bundle=new Bundle();
                bundle.putString("id", String.valueOf(id));
                intent.putExtras(bundle);
                mContext.startActivity(intent);

            }
        });
        return view;
    }
    public void addItem(ShangjiaInfo info) {
        mListData.add(info);
    }

    public void addItems(List<ShangjiaInfo> lists) {
        mListData.addAll(lists);
        notifyDataSetChanged();

    }
    public void clear() {
        mListData.clear();
    }
    class ViewHold {
        TextView shangjiainfo;
    }
}
